<?php
/**
 * 访客脚印
 * 
 * @author jiaowei <jiaowei@178.com>
 * @version $Id: footprint.php 13644 2011-02-16 09:03:17Z jiaowei $
 */
class friends_controller_footprint extends SnsController
{
	/**
	 * friends2_model_footprint
	 *
	 * @var object
	 */
	private $_mod = null;
	
	/**
	 * 模块缓存前缀
	 *
	 * @var str
	 */
	private $_module = null;
	
	public function __construct($app, $controller)
	{
		parent::__construct($app, $controller);
		$this->_mod = Model::factory('friends_model_footprint'); 
		$this->headerView = $this->viewDir."/_header.phtml";
		$this->frameView = VIEW_PATH.'/sns/index.phtml';
		
		$this->_module = $app . '_' . $controller;
	}
	
	/**
	 * 谁来看过我
	 *
	 * @display true
	 */
	public function get_list_action()
	{
		$uid = $this->getUid();
		if (!$uid) {
			$this->view = null;
			return ;
		}
		
		$currPage = _('page', 1);
		/*
		list($data, $pageInfo) = MemRow::getPageList($this->_module, $currPage, friends_model_footprint::PAGE_SIZE, array(
			array($this->_mod, 'getInListId', array($uid)),
			array($this->_mod, 'getRow')
		));
		*/
		
		list($uids, $times) = MemRow::getListId(array($this->_mod, 'getInListId2', array($uid)));
		list($pageId, $pageInfo) = MemRow::getPageListId($uids, $currPage, friends_model_footprint::PAGE_SIZE);
		//$data = MemRow::mget($this->_module, $pageId, array($this->_mod, 'getRow'));
		$data = friends_util_cache::getUserInfo($pageId);
		foreach ((array) $data as $k => $v) {
			if (isset($times[$v['id']])) {
				$data[$k]['visit_time'] = $times[$v['id']];
			}
		}

		$this->pageInfo = $pageInfo;
		$this->ret 		= $data;
	}
	
	/**
	 * 我去看过谁
	 *
	 * @display true
	 */
	public function get_out_list_action()
	{
		$uid = $this->getUid();
		if (!$uid) {
			$this->view = null;
			return ;
		}
		
		$currPage = _('page', 1);
		/*
		list($data, $pageInfo) = MemRow::getPageList($this->_module, $currPage, friends_model_footprint::PAGE_SIZE, array(
			array($this->_mod, 'getOutListId', array($uid)),
			array($this->_mod, 'getRow')
		));
		*/
		
		list($uids, $times) = MemRow::getListId(array($this->_mod, 'getOutListId2', array($uid)));
		list($pageId, $pageInfo) = MemRow::getPageListId($uids, $currPage, friends_model_footprint::PAGE_SIZE);
		$data = friends_util_cache::getUserInfo($pageId);
		foreach ((array) $data as $k => $v) {
			if (isset($times[$v['id']])) {
				$data[$k]['visit_time'] = $times[$v['id']];
			}
		}
		
		$this->pageInfo = $pageInfo;
		$this->ret 		= $data;	
	}
	
	/**
	 * 首页嵌入
	 *
	 * @display false
	 */
	public function embed_action(){
		$uid = $this->getUid();
		
		if (!$uid) {
			$this->view = null;
			return ;
		}
		/*
		list($data, $pageInfo) = MemRow::getPageList($this->_module, 1, 18, array(
			array($this->_mod, 'getInListId', array($uid)),
			array($this->_mod, 'getRow')
		));
		*/
		
		list($uids, $times) = MemRow::getListId(array($this->_mod, 'getInListId2', array($uid)));
		list($pageId, $pageInfo) = MemRow::getPageListId($uids, 1, 18);
		$data = friends_util_cache::getUserInfo($pageId);

		foreach ((array) $data as $k => $v) {
			if (isset($times[$v['id']])) {
				$data[$k]['visit_time'] = $times[$v['id']];
			}
		}
		
		$this->rows = $data;
		$this->_renderSingle();
	}
	
	/**
	 * 记录足迹
	 *
	 * @display false
	 */
	public function trace_action(){
		$this->view = null;
		if(self::$login and $this->uid and self::$login['uid']!=$this->uid){
			$privacy = index_util_prop::getDefaultSrank($this->uid, 'footprint');
			$yes = true;
			if($privacy == index_util_prop::FOOTPRINT_NO ){
				$yes = friends_util_checker::isFriend(self::$login['uid'], $this->uid);
			}
			if($yes){
				$id = $this->_mod->save(self::$login['uid'], self::$login['nickname'], $this->uid, 
					Cache::cacheFunction(array($this->SL, "soap_query_info"), array($this->uid)));
//				$this->o->clearCache(self::$login['uid'], $this->uid, self::PAGESIZE );
				//CacheManager::getInstance()->refresh($this->app, $this->controller);
				
				MemRow::mdel(MemRow::genRowKey($this->_module, $id));
				
				MemRow::mdel(MemRow::genListKey(array($this->_mod, 'getOutListId', array(self::$login['uid']))));
				MemRow::mdel(MemRow::genListKey(array($this->_mod, 'getInListId', array($this->uid))));
				
				//MemRow::removeListIdCache($this->_module, array($this->_mod, 'getOutListId', array(self::$login['uid'])));
				//MemRow::removeListIdCache($this->_module, array($this->_mod, 'getInListId', array($this->uid)));
			}
		}
	}
}